define(["i18n"], function (i18n) {
    var userListCtrl = ["$scope", "userService", function ($scope, userService) {
        $scope.word = "我是list";
        $scope.result = "";
        $scope.userList = [];
        $scope.index = "";
        $scope.user1 = {
            nickName: "",
            userName: "",
            password: ""
        };
        $scope.initUser = function () {
            $scope.user = {
                nickName: "",
                userName: "",
                password: ""
            };
        }
        $scope.transferValue = function (user, index) {
            $scope.user = user;
            $scope.index = index;
        }
        $scope.getUsers = function () {
            userService.getUsers().then(function (response) {
                $scope.userList = response.data;
            })
        }

        $scope.deleteUser = function () {
            userService.deleteUser($scope.user.id).then(function (response) {
                $scope.userList.splice($scope.index, 1);
                alert(response.data.message);
            })
        }

        $scope.updateUser = function () {
            var user = $scope.user;
            userService.updateUser(user).then(function (response) {
                $scope.userList.splice($scope.index, 1, user);//替换
                alert(response.data.message);
            })
        }

        $scope.addUser = function () {
            var user2 = $scope.user1;
            userService.addUser(user2).then(function (value) {
                $scope.user1 = {
                    nickName: "",
                    userName: "",
                    password: ""
                };
                if (value.status === 200) {
                    $scope.userList.push(user2);
                    alert("添加用户 成功");
                }
            })
        }
        $scope.getUsers();
    }];

    //引用框架定义的方法
    angular.module("user").registerController("userListCtrl", userListCtrl);
    //return userCtrl;
})